//
//  HJInOrOutRecordsController.m
//  Keping
//
//  Created by a on 2017/8/28.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJInOrOutRecordsController.h"
#import "HJInOrOutRecordsRequest.h"
#import "HJInOrOutRecordsModel.h"
#import <MJRefresh/MJRefresh.h>
#import "HJInOutViewCell.h"

@interface HJInOrOutRecordsController ()<UITableViewDelegate,UITableViewDataSource>{
    
}

/** tableView */
@property(nonatomic,weak)UITableView*tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger pageIndex;
@end

@implementation HJInOrOutRecordsController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pageIndex = 1;
    [self setUI];
}
-(void)setUI
{
    self.navigationItem.title=@"In/Out";
    UITableView*tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.tableFooterView = [UIView new];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    WEAKSELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pageIndex = 1;
        [weakSelf getData];
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.pageIndex += 1;
        [weakSelf getData];
    }];
    [tableView.mj_header beginRefreshing];
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)getData {
    [super getData];
    WEAKSELF
    HJInOrOutRecordsRequest *request = [[HJInOrOutRecordsRequest alloc] init];
    request.params = @{@"page":@(self.pageIndex),@"pageSize":@"20"};
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        NSDictionary *dict = request.responseObject;
        NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
        if (status == 200) {
            if (weakSelf.pageIndex == 1) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSArray *array = [dict objectForKey:@"entranceOperateHiViews"];
            for (NSDictionary *tmp in array) {
                HJInOrOutRecordsModel *model = [HJInOrOutRecordsModel modelWithDictionary:tmp];
                [weakSelf.dataArray addObject:model];
            }
            [weakSelf.tableView reloadData];
        } else {
            [weakSelf showErrorText:[dict objectForKey:@"msg"] errCode:status];
        }
        
        if (weakSelf.pageIndex == 1) {
            [weakSelf.tableView.mj_header endRefreshing];
        } else {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        if (weakSelf.pageIndex == 1) {
            [weakSelf.tableView.mj_header endRefreshing];
        } else {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [weakSelf showError:request.error];
    }];
}

#pragma mark --UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJInOutViewCell *cell=[HJInOutViewCell cellWithTableView:tableView];
    HJInOrOutRecordsModel *model = self.dataArray[indexPath.row];
    cell.inOrOutRecords = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return K_FactorH(90);
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
